package avicit.bdp.lineage.druid.util;

import avicit.bdp.lineage.druid.model.FieldLineageModel;
import avicit.bdp.lineage.druid.model.FieldNameModel;
import avicit.bdp.lineage.druid.model.TableLineageModel;
import avicit.bdp.lineage.druid.model.TableNameModel;
import avicit.bdp.lineage.druid.model.TableNode;
import avicit.bdp.lineage.druid.model.TreeNode;
import com.fasterxml.jackson.annotation.JsonInclude;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 血缘上下文
 * </p>
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
public class LineageContext {
    private Map<String, TreeNode<TableNode>> withTreeNodeMap = new HashMap<>();
    private Map<String, TableNode> tableNodeMap = new HashMap<>();
    private FieldLineageModel currentFieldModel;

    private List<TableLineageModel> tableNodeList = new ArrayList<>();
    private List<FieldLineageModel> fieldNodeList = new ArrayList<>();

    public List<TableLineageModel> getTableNodeList() {
        return tableNodeList;
    }

    public void setTableNodeList(List<TableLineageModel> tableNodeList) {
        this.tableNodeList = tableNodeList;
    }

    public List<FieldLineageModel> getFieldNodeList() {
        return fieldNodeList;
    }

    public void setFieldNodeList(List<FieldLineageModel> fieldNodeList) {
        this.fieldNodeList = fieldNodeList;
    }

    public Map<String, TreeNode<TableNode>> getWithTreeNodeMap() {
        return withTreeNodeMap;
    }

    public void setWithTreeNodeMap(Map<String, TreeNode<TableNode>> withTreeNodeMap) {
        this.withTreeNodeMap = withTreeNodeMap;
    }

    public Map<String, TableNode> getTableNodeMap() {
        return tableNodeMap;
    }

    public void setTableNodeMap(Map<String, TableNode> tableNodeMap) {
        this.tableNodeMap = tableNodeMap;
    }

    public FieldLineageModel getCurrentFieldModel() {
        return currentFieldModel;
    }

    public void setCurrentFieldModel(FieldLineageModel currentFieldModel) {
        this.currentFieldModel = currentFieldModel;
    }

    public void print() {
        System.out.println("###############");
        System.out.println("开始输出表血缘：");
        for (TableLineageModel model : tableNodeList) {
            TableNameModel targetTable = model.getTargetTable();
            System.out.println("目标表：" + targetTable.getDbName() + " " + targetTable.getTableName());
            HashSet<TableNameModel> sourceTables = model.getSourceTables();
            for (TableNameModel sourceTable : sourceTables) {
                System.out.println("来源表：" + sourceTable.getDbName() + " " + sourceTable.getTableName());
            }
            System.out.println("------------------");
        }
        System.out.println("开始输出字段血缘：");
        for (FieldLineageModel model : fieldNodeList) {
            FieldNameModel targetField = model.getTargetField();
            System.out.println("目标字段：" + targetField.getDbName() + " " + targetField.getTableName() + " " + targetField.getFieldName());
            HashSet<FieldNameModel> sourceFields = model.getSourceFields();
            for (FieldNameModel sourceField : sourceFields) {
                System.out.println("来源字段：" + sourceField.getDbName() + " " + sourceField.getTableName() + " " + sourceField.getFieldName());
            }
            System.out.println("------------------");
        }
    }
}
